package practice1;

import java.util.*;

/**
 * Take 2 strings s1 and s2 including only letters from ato z. Return a new sorted string, the longest possible,
 * containing distinct letters,
 * each taken only once - coming from s1 or s2. #Examples: ``` a = "xyaabbbccccdefww" b = "xxxxyyyyabklmopq" longest(a, b) -> "abcdefklmopqwxy"
 * a = "abcdefghijklmnopqrstuvwxyz" longest(a, a) -> "abcdefghijklmnopqrstuvwxyz" ```
 */
public class TwoToOne {
    public static String longest(String s1, String s2) {
        StringBuilder input = new StringBuilder(s1 + s2);
        char[] chars = input.toString().toCharArray();
        Arrays.sort(chars);
        String result = String.valueOf(chars).replaceAll("(.)(?=.*\\1)", "");
        return result;
    }

    public static String bestLongest(String s1, String s2) {
        String s = s1 + s2;
        return s.chars().distinct().sorted().collect(StringBuilder::new, StringBuilder::appendCodePoint,
                StringBuilder::append).toString();
    }
}
